<?php
/**
 * 用户控制器
 */

namespace app\api\controller;

use app\api\exception\ApiServiceException;
use app\api\service\UserService;
use app\common\model\User;
use app\common\validate\UserValidate;
use hg\apidoc\annotation as Apidoc;
use think\response\Json;

/**
 * @Apidoc\Title("用户")
 * @Apidoc\Group("index")
 */
class UserController extends ApiBaseController {
	// 设置添加用户3秒内无法重复请求
	protected array $throttleAction = [
		'api/user/add' => 3,
	];

	/**
	 * 列表
	 * @param UserService $service
	 * @return Json
	 */
	public function index(UserService $service): Json {
		try {
			$data = $service->getList($this->param, $this->page, $this->limit);
			$result = [
				'user' => $data,
			];

			return api_success($result);
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 添加
	 *
	 * @param UserValidate $validate
	 * @param UserService $service
	 * @return Json
	 */
	public function add(UserValidate $validate, UserService $service): Json {
		$check = $validate->scene('api_add')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		$result = $service->createData($this->param);

		return $result ? api_success() : api_error();
	}

	/**
	 * @Apidoc\Title("用户详情")
	 * @Apidoc\Desc("用户详情")
	 * @Apidoc\Method("GET")
	 * @Apidoc\Url("/api/user/info")
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function info(UserValidate $validate, UserService $service): Json {
		$check = $validate->scene('api_info')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {

			$result = $service->getDataInfo($this->uid);
			return api_success([
				'user' => $result,
			]);

		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * @Apidoc\Title("修改密码")
	 * @Apidoc\Desc("修改密码")
	 * @Apidoc\Method("POST")
	 * @Apidoc\Url("/api/user/edit")
	 * @Apidoc\Param("password", type="string", desc="竞标内容"),
	 * @Apidoc\Returned("data",        type="int",                 desc="desc")
	 */
	public function edit(UserService $service, UserValidate $validate): Json {

		try {
			$service->updateData($this->uid, $this->param);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 删除
	 * @param UserService $service
	 * @param UserValidate $validate
	 * @return Json
	 */
	public function del(UserService $service, UserValidate $validate): Json {
		$check = $validate->scene('api_del')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->deleteData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 禁用
	 * @param UserService $service
	 * @param UserValidate $validate
	 * @return Json
	 */
	public function disable(UserService $service, UserValidate $validate): Json {
		$check = $validate->scene('api_disable')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->disableData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}

	/**
	 * 启用
	 * @param UserService $service
	 * @param UserValidate $validate
	 * @return Json
	 */
	public function enable(UserService $service, UserValidate $validate): Json {
		$check = $validate->scene('api_enable')->check($this->param);
		if (!$check) {
			return api_error($validate->getError());
		}

		try {
			$service->enableData($this->id);
			return api_success();
		} catch (ApiServiceException $e) {
			return api_error($e->getMessage());
		}
	}
	// public function logout() {
	// 	User::where('id', $this->uid)->update(['live_time' => 0]);
	// }
}
